//
//  BLRoomPickView.m
//  Byelone
//
//  Created by Qi Liu on 2021/2/8.
//

#import "BLRoomPickView.h"
#import "PGPickerView.h"



typedef void(^DatePickBlock)(BLCommon_Model_AlertItem * _Nullable dateDic);

@interface RoomPickCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIView *baseview;
@property (weak, nonatomic) IBOutlet UILabel *desiLabel;

@end

@implementation RoomPickCell


-(void)awakeFromNib{
    [super awakeFromNib];
    self.desiLabel.textColor = [UIColor chColorWithHexString:@"#888888"];
    self.desiLabel.font = [UIFont systemFontOfSize:15];
    self.baseview.layer.cornerRadius = 10;
    self.baseview.layer.masksToBounds = YES;
    
}

-(instancetype)initWithCoder:(NSCoder *)coder{
    if (self = [super initWithCoder:coder]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

@end

@interface BLRoomPickView ()<UITableViewDelegate,UITableViewDataSource>
{

}
@property(nonatomic,strong) UIView * baseView;
@property(nonatomic,strong) UITableView * listView;
@property(nonatomic,strong) UILabel * titleLabel;
@property(nonatomic,strong) UIButton * cancelBtn;
@property(nonatomic,strong) UIButton * sureBtn;
@property (nonatomic, assign) NSInteger provinceIndex,cityIndex;
@property(nonatomic,copy) DatePickBlock  dateBlock;

@property (nonatomic, strong) NSArray * bigArray;
@property (nonatomic, assign) CGFloat ListH;


@end

@implementation BLRoomPickView



+(void)showRoomPickViewTitle:(NSString *)title array:(NSArray *)contentArray select:(void(^)(BLCommon_Model_AlertItem * dateDic))block{
    BLRoomPickView * pickView = [[BLRoomPickView alloc]initWithFrame:BL_WINDOW_FRAME];
    pickView.dateBlock = block;
    pickView.title = title;
    pickView.bigArray = contentArray;
    [BL_WINDOW addSubview:pickView];
}

- (void)setBigArray:(NSArray *)bigArray{
    _bigArray = bigArray;
    self.ListH = 50*bigArray.count + 50 ;
    if (bigArray.count>=5) {
        self.ListH = 300;
    }
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.ListH+BL_BOTTOM_MARGIN);
    }];
    [self.listView reloadData];
}

- (void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = BL_COLOR_ALPHA;
        [self setUp];
    }
    return self;
}

#define CELLIEND @"RoomPickCell"
-(void)setUp{
    UIView * baseView = [[UIView alloc]init];
    baseView.backgroundColor = [UIColor whiteColor];
    self.baseView = baseView;
    [self addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(300+BL_BOTTOM_MARGIN);
    }];
    baseView.layer.cornerRadius = 10;
    baseView.layer.masksToBounds = YES;
    UIView *view = [[UIView alloc]init];
    view.backgroundColor =[UIColor whiteColor];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.mas_equalTo(10);
    }];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    titleLabel.backgroundColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor chColorWithHexString:@"#555555"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [baseView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(25);
    }];
    self.titleLabel = titleLabel;
    
//    CGFloat lisY = CGRectGetMaxY(titleLabel.frame)+ 15;
    UITableView * listView = [[UITableView alloc]init];
    listView.estimatedRowHeight = 40;
    listView.showsVerticalScrollIndicator = NO;
    [listView registerNib:[UINib nibWithNibName:CELLIEND bundle:nil] forCellReuseIdentifier:CELLIEND];
    listView.delegate = self;
    listView.dataSource = self;
    listView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [baseView addSubview:listView];
    [listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
        make.bottom.mas_equalTo(-BL_BOTTOM_MARGIN);
    }];
    self.listView = listView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.bigArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   static NSString * cellID = @"RoomPickCell";
    RoomPickCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:CELLIEND owner:nil options:nil]lastObject];
    }
    BLCommon_Model_AlertItem *item = [self.bigArray objectAtIndex:indexPath.row];
    cell.desiLabel.text = item.text;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RoomPickCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.baseview.backgroundColor = [UIColor chColorWithHexString:@"#E7E8FF"];
    cell.desiLabel.textColor = [UIColor chColorWithHexString:@"#6C72F5"];
    
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    indexPath.
//    cell.basevie
    if (_dateBlock) {
        BLCommon_Model_AlertItem *item = [self.bigArray objectAtIndex:indexPath.row];
        _dateBlock(item);
    }
    
    [self disMissView];
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    RoomPickCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.baseview.backgroundColor = [UIColor whiteColor];
    cell.desiLabel.textColor = [UIColor chColorWithHexString:@"#888888"];
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



@end
